package com.hznc.wcs.controller.bpm;

import com.diboot.core.vo.JsonResult;
import com.diboot.core.vo.Pagination;
import com.diboot.iam.annotation.BindPermission;
import com.diboot.iam.annotation.Log;
import com.diboot.iam.annotation.OperationCons;
import com.hznc.wcs.service.bpm.BpmModelService;
import com.hznc.wcs.vo.bpm.*;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description : 流程模型接口
 * @Date : 2024/6/20
 * @Author : ChenJin
 * @Version : v1.0.0
 **/
@Slf4j
@RequestMapping("/bpm-model")
@BindPermission(name = "流程模型",code = "BpmModel")
@RestController
public class BpmModelController{

    @Resource
    private BpmModelService bpmModelService;

    /**
     * 分页查询流程模型列表
     * @param reqVO
     * @param pagination
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_LIST)
    @BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
    @GetMapping("/getModelPage")
    public JsonResult<?> getModelPage(BpmModelReqVO reqVO, Pagination pagination){
        //获取模型
        List<BpmModelRespVO> bpmModelRespVOList = bpmModelService.getBpmModelPage(reqVO,pagination);
        return JsonResult.OK(bpmModelRespVOList).bindPagination(pagination);
    }

    /**
     * 查询流程模型详情
     * @param id
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DETAIL)
    @BindPermission(name = OperationCons.LABEL_DETAIL, code = OperationCons.CODE_READ)
    @GetMapping("/getModelInfo/{id}")
    public JsonResult<?> getModelInfo(@PathVariable("id") String id){
        //获取模型
        BpmModelRespVO bpmModelRespVO = bpmModelService.getBpmModelInfo(id);
        return JsonResult.OK(bpmModelRespVO);
    }

    /**
     * 新增流程模型
     * @param createReqVO
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_CREATE)
    @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/createModel")
    public JsonResult<?> createModel(@Valid  @RequestBody BpmModelCreateReqVO createReqVO){
        String modelId = bpmModelService.createModel(createReqVO, createReqVO.getBpmnXml());
        return JsonResult.OK(modelId);
    }


    /**
     * 修改流程模型
     * @param updateReqVO
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_UPDATE)
    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PutMapping("/updateModel/{mid}")
    public JsonResult<?> updateModel(@PathVariable("mid") String mid,@Valid  @RequestBody BpmModelUpdateReqVO updateReqVO){
        String modelId = bpmModelService.updateModel(updateReqVO, updateReqVO.getBpmnXml());
        return JsonResult.OK(modelId);
    }

    /**
     * 删除流程模型
     * @param id
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DELETE)
    @BindPermission(name = OperationCons.LABEL_DELETE, code = OperationCons.CODE_WRITE)
    @DeleteMapping("/deleteModel/{id}")
    public JsonResult<?> deleteModel(@PathVariable("id") String id){
        bpmModelService.deleteModel(id);
        return JsonResult.OK();
    }

    /**
     * 发布流程模型
     * @param id
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_UPDATE)
    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PutMapping("/deployModel/{id}")
    public JsonResult<?> deployModel(@PathVariable("id") String id) {
        String definitionId = bpmModelService.deployModel(id);
        return JsonResult.OK(definitionId);
    }

    /**
     * 激活或挂起流程模型
     * @param reqVO
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_UPDATE)
    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PutMapping("/updateModelState")
    public JsonResult<?> updateModelState(@RequestBody BpmModelUpdateStateReqVO reqVO){
        bpmModelService.updateModelState(reqVO.getId(), reqVO.getState());
        return JsonResult.OK();
    }

    public static void main(String[] args) {
        String bpmXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><definitions xmlns=\"http://www.omg.org/spec/BPMN/20100524/MODEL\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:bpmndi=\"http://www.omg.org/spec/BPMN/20100524/DI\" xmlns:flowable=\"http://flowable.org/bpmn\" xmlns:di=\"http://www.omg.org/spec/DD/20100524/DI\" xmlns:dc=\"http://www.omg.org/spec/DD/20100524/DC\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" targetNamespace=\"http://www.activiti.org/processdef\"><process id=\"test_process\" name=\"测试流程\" isExecutable=\"true\"><startEvent id=\"Event_0ch1hk1\" name=\"开始\"><extensionElements><flowable:formData /></extensionElements><outgoing>Flow_0h258le</outgoing></startEvent><userTask id=\"Activity_1dew5mv\" name=\"审批节点1\" flowable:candidateStrategy=\"30\" flowable:candidateParam=\"1\"><extensionElements><flowable:formData /></extensionElements><incoming>Flow_0h258le</incoming><outgoing>Flow_04mo6c7</outgoing></userTask><userTask id=\"Activity_087ck5b\" name=\"审批节点2\" flowable:candidateStrategy=\"30\" flowable:candidateParam=\"103\"><extensionElements><flowable:formData /></extensionElements><incoming>Flow_04mo6c7</incoming><outgoing>Flow_1hlsewk</outgoing></userTask><userTask id=\"Activity_1s390vt\" name=\"审批节点3\" flowable:candidateStrategy=\"30\" flowable:candidateParam=\"104\"><extensionElements><flowable:formData /></extensionElements><incoming>Flow_1hlsewk</incoming><outgoing>Flow_11egfy7</outgoing></userTask><endEvent id=\"Event_055vm13\"><incoming>Flow_11egfy7</incoming></endEvent><sequenceFlow id=\"Flow_0h258le\" sourceRef=\"Event_0ch1hk1\" targetRef=\"Activity_1dew5mv\" /><sequenceFlow id=\"Flow_04mo6c7\" sourceRef=\"Activity_1dew5mv\" targetRef=\"Activity_087ck5b\" /><sequenceFlow id=\"Flow_1hlsewk\" sourceRef=\"Activity_087ck5b\" targetRef=\"Activity_1s390vt\" /><sequenceFlow id=\"Flow_11egfy7\" sourceRef=\"Activity_1s390vt\" targetRef=\"Event_055vm13\" /></process><bpmndi:BPMNDiagram id=\"BPMNDiagram\"><bpmndi:BPMNPlane id=\"test_process_di\" bpmnElement=\"test_process\"><bpmndi:BPMNEdge id=\"Flow_0h258le_di\" bpmnElement=\"Flow_0h258le\"><di:waypoint x=\"630\" y=\"88\" /><di:waypoint x=\"630\" y=\"150\" /></bpmndi:BPMNEdge><bpmndi:BPMNEdge id=\"Flow_04mo6c7_di\" bpmnElement=\"Flow_04mo6c7\"><di:waypoint x=\"630\" y=\"230\" /><di:waypoint x=\"630\" y=\"320\" /></bpmndi:BPMNEdge><bpmndi:BPMNEdge id=\"Flow_1hlsewk_di\" bpmnElement=\"Flow_1hlsewk\"><di:waypoint x=\"630\" y=\"400\" /><di:waypoint x=\"630\" y=\"480\" /></bpmndi:BPMNEdge><bpmndi:BPMNEdge id=\"Flow_11egfy7_di\" bpmnElement=\"Flow_11egfy7\"><di:waypoint x=\"630\" y=\"560\" /><di:waypoint x=\"630\" y=\"642\" /></bpmndi:BPMNEdge><bpmndi:BPMNShape id=\"Event_0ch1hk1_di\" bpmnElement=\"Event_0ch1hk1\"><dc:Bounds x=\"612\" y=\"52\" width=\"36\" height=\"36\" /><bpmndi:BPMNLabel><dc:Bounds x=\"619\" y=\"22\" width=\"23\" height=\"14\" /></bpmndi:BPMNLabel></bpmndi:BPMNShape><bpmndi:BPMNShape id=\"Activity_1dew5mv_di\" bpmnElement=\"Activity_1dew5mv\"><dc:Bounds x=\"580\" y=\"150\" width=\"100\" height=\"80\" /></bpmndi:BPMNShape><bpmndi:BPMNShape id=\"Activity_087ck5b_di\" bpmnElement=\"Activity_087ck5b\"><dc:Bounds x=\"580\" y=\"320\" width=\"100\" height=\"80\" /></bpmndi:BPMNShape><bpmndi:BPMNShape id=\"Activity_1s390vt_di\" bpmnElement=\"Activity_1s390vt\"><dc:Bounds x=\"580\" y=\"480\" width=\"100\" height=\"80\" /></bpmndi:BPMNShape><bpmndi:BPMNShape id=\"Event_055vm13_di\" bpmnElement=\"Event_055vm13\"><dc:Bounds x=\"612\" y=\"642\" width=\"36\" height=\"36\" /></bpmndi:BPMNShape></bpmndi:BPMNPlane></bpmndi:BPMNDiagram></definitions>";
    }

}
